package com.cjj.common.framework.excel.handler;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.cjj.common.utils.ExportExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author DoyuTu
 */
@Slf4j
public class RedMarkCellWriteHandler implements CellWriteHandler {

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        ExportExcelUtil.Mark mark = ExportExcelUtil.Mark.RED;
        setCellMark(writeSheetHolder, writeTableHolder, cellDataList, cell, head, relativeRowIndex, isHead, mark);
    }

    private void setCellMark(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead, ExportExcelUtil.Mark mark) {
        if (cell.getCellTypeEnum() == CellType.STRING && StrUtil.isNotBlank(cell.getStringCellValue())) {
            CreationHelper createHelper = writeSheetHolder.getSheet().getWorkbook().getCreationHelper();
            String stringCellValue = cell.getStringCellValue();
            int preMarkLength = mark.getPre().length();
            int postMarkLength = mark.getPost().length();
            int totalMarkLength = preMarkLength + postMarkLength;
            List<ExportExcelUtil.ExcelIndex> list = new ArrayList<>();
            while (true) {
                int lastEndIndexOf = stringCellValue.lastIndexOf(mark.getPost());
                int lastStartIndexOf = stringCellValue.lastIndexOf(mark.getPre());
                if (lastEndIndexOf < 0 || lastStartIndexOf < 0) {
                    break;
                }
                ExportExcelUtil.ExcelIndex indexBean = new ExportExcelUtil.ExcelIndex();
                indexBean.setEndIndex(totalMarkLength + lastEndIndexOf);
                indexBean.setStartIndex(lastStartIndexOf);
                stringCellValue = stringCellValue.substring(0, lastStartIndexOf) + stringCellValue.substring(lastStartIndexOf, lastEndIndexOf + 6).replaceAll(mark.getPre() + "(.*)" + mark.getPost(), "$1") + stringCellValue.substring(lastEndIndexOf + 6);
                list.add(indexBean);
            }
            if (!list.isEmpty()) {
                for (int i = 0; i < list.size(); i++) {
                    ExportExcelUtil.ExcelIndex indexBean = list.get(i);
                    indexBean.setStartIndex(indexBean.getStartIndex() - totalMarkLength * (list.size() - i - 1));
                    indexBean.setEndIndex(indexBean.getEndIndex() - totalMarkLength * (list.size() - i) - preMarkLength);
                }
                RichTextString richTextString = createHelper.createRichTextString(stringCellValue);
                String fontName = "宋体";
                short fontHeightInPoints = (short) 10;
                Font font = writeSheetHolder.getSheet().getWorkbook().createFont();
                font.setColor(mark.getColor());
                font.setFontName(fontName);
                font.setFontHeightInPoints(fontHeightInPoints);
                Font normalFont = writeSheetHolder.getSheet().getWorkbook().createFont();
                normalFont.setFontName(fontName);
                normalFont.setFontHeightInPoints(fontHeightInPoints);
                richTextString.applyFont(normalFont);
                for (ExportExcelUtil.ExcelIndex indexBean : list) {
                    richTextString.applyFont(indexBean.getStartIndex(), indexBean.getEndIndex(), font);
                }
                cell.setCellValue(richTextString);
            }
        }
    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }
}
